/**
 * Copyright (c) 2014-2015 ENZO 熊强强  (xqenzo@163.com).
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 	
 */

package cn.edu.xyc.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import cn.edu.xyc.domain.EntityPojo;
import cn.edu.xyc.service.UserService;
import cn.edu.xyc.util.XlsUtils;

/**
 * <p>
 * Title: UpfileController.java
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Copyright: Copyright (c) 2015
 * </p>
 * 
 * @author ENZO E-mail:xqenzo@163.com
 * @date 创建时间：2015年10月17日 下午8:05:39
 * @version 1.0
 */
@Controller
public class UpfileController {
    
    private @Value(value = "#{config[VirtualPath]}") String VirtualPath;
    private @Value(value = "#{config[RealPath]}") String RealPath;
    
    
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/admin/uploadfile")
    public String upload(
	    @RequestParam(value = "file", required = false) MultipartFile file,
	    HttpServletRequest request, ModelMap model) {
	String contextPath = request.getServletContext().getContextPath();
	String fileName = file.getOriginalFilename();
	String realpath =this.RealPath;
	String urlpath =this.VirtualPath;
	File targetFile = new File(realpath, fileName);

	if (!targetFile.exists()) {
	    targetFile.mkdirs();
	}

	// 保存
	try {
	    file.transferTo(targetFile);
	} catch (Exception e) {
	    e.printStackTrace();
	}

	List<EntityPojo> arrayList = getxlslist(targetFile);

	model.addAttribute("filename", fileName);
	model.addAttribute("filepath", contextPath + urlpath);
	model.addAttribute("list", arrayList);
	return "main";
    }

    @RequestMapping(value = "/admin/savedata")
    public String savedata(HttpServletRequest request) {
	String path = request.getParameter("path");
	String ablovpath =this.RealPath;
	File dir = new File(ablovpath, path);
	List<EntityPojo> getxlslist = getxlslist(dir);
	
	//将服务器中的数据存到数据库中
	this.userService.addinfo(getxlslist);
	return "redirect:/admin/list.do";
    }

    private List<EntityPojo> getxlslist(File f) {
	List<EntityPojo> arrayList = new ArrayList<EntityPojo>();

	try {
	    arrayList = XlsUtils.readXls(f.getAbsolutePath());
	} catch (IOException e) {
	    e.printStackTrace();
	}
	return arrayList;
    }
    
    @RequestMapping(value="/admin/upload")
    public String upload(){
	System.out.println("upload");
	return "upload";
    }

}
